# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Maintainer: Michael Pirogov <vbnet.ru@gmail.com>
pkgname=moosefs
pkgver=3.0.117
pkgrel=2
pkgdesc="Open Source, Petabyte, Fault-Tolerant, Highly Performing, Scalable Network Distributed File System"
url="https://moosefs.com"
arch="all"
license="GPL-2.0-only"
makedepends="autoconf automake zlib-dev libpcap-dev libtool fuse3-dev python3-dev"
pkgusers="mfs"
pkggroups="mfs"
subpackages="$pkgname-doc $pkgname-static $pkgname-client $pkgname-master
	$pkgname-chunkserver $pkgname-metalogger $pkgname-cgi::noarch
	$pkgname-cgiserv::noarch
	$pkgname-metalogger-openrc:metalogger_openrc
	$pkgname-master-openrc:master_openrc
	$pkgname-chunkserver-openrc:chunkserver_openrc
	$pkgname-cgiserv-openrc:cgiserv_openrc"
source="$pkgname-$pkgver.tar.gz::http://ppa.moosefs.com/src/moosefs-$pkgver-1.tar.gz
	$pkgname-cgiserv.initd
	$pkgname-cgiserv.confd
	$pkgname-master.initd
	$pkgname-metalogger.initd
	$pkgname-chunkserver.initd"

prepare() {
	default_prepare
	autoreconf -fi
}

build() {
	# Configure and remove systemd stuff
	./configure --prefix=/usr \
		--with-systemdsystemunitdir=no \
		--sysconfdir=/etc \
		--with-default-user=mfs \
		--with-default-group=mfs \
		--disable-dependency-tracking \
		--disable-silent-rules \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--datadir=/usr/share \
		--localstatedir=/var/lib \
		--libdir=/usr/lib
	make
}

check() {
	cd mfstests
	./mfstest_clocks
	./mfstest_crc32
	./mfstest_datapack
	./mfstest_delayrun
}

package() {
	make DESTDIR=$pkgdir install
	chown -R mfs:mfs "$pkgdir"/var/lib/mfs
}

_mv_files() {
	local i
	for i in "$@"; do
		case $i in
		*/*) mkdir -p "$subpkgdir"/${i%/*};;
		*) mkdir -p "$subpkgdir";;
		esac
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

master() {
	pkgdesc="MooseFS master server"
	depends="moosefs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfsmaster \
		usr/sbin/mfsmetadump \
		usr/sbin/mfsmetarestore \
		etc/mfs/mfsexports.cfg.sample \
		etc/mfs/mfstopology.cfg.sample \
		etc/mfs/mfsmaster.cfg.sample \
		var/lib/mfs/metadata.mfs.empty
		chown -R $pkgusers:$pkggroups "$subpkgdir"/var/lib/mfs
}

master_openrc() {
	default_openrc
	cd "$pkgdir"
	install -Dm755 "$srcdir"/moosefs-master.initd "$subpkgdir"/etc/init.d/moosefs-master
}

metalogger() {
	pkgdesc="MooseFS metadata replication server"
	depends="moosefs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfsmetalogger \
		etc/mfs/mfsmetalogger.cfg.sample
}

metalogger_openrc() {
	default_openrc
	cd "$pkgdir"
	install -Dm755 "$srcdir"/moosefs-metalogger.initd "$subpkgdir"/etc/init.d/moosefs-metalogger
}

chunkserver() {
	pkgdesc="MooseFS data server"
	depends="moosefs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfschunkserver \
		etc/mfs/mfschunkserver.cfg.sample \
		etc/mfs/mfshdd.cfg.sample
}

chunkserver_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/moosefs-chunkserver.initd "$subpkgdir"/etc/init.d/moosefs-chunkserver
}

client() {
	pkgdesc="MooseFS client"
	depends="fuse3"
	cd "$pkgdir"
	_mv_files \
		usr/bin \
		sbin/mount.* \
		etc/mfs/mfsmount.cfg.sample

	mv "$subpkgdir"/sbin "$subpkgdir"/usr/
}

cgi() {
	pkgdesc="MooseFS CGI Monitor"
	depends="python3"
	cd "$pkgdir"
	_mv_files usr/share/mfscgi
}

cgiserv() {
	pkgdesc="Simple CGI-capable HTTP server to run MooseFS CGI Monitor"
	depends="$pkgname-cgi=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files usr/sbin/mfscgiserv
}

cgiserv_openrc() {
	default_openrc
	install -Dm755 "$srcdir"/moosefs-cgiserv.initd "$subpkgdir"/etc/init.d/moosefs-cgiserv
	install -Dm644 "$srcdir"/moosefs-cgiserv.confd "$subpkgdir"/etc/conf.d/moosefs-cgiserv
}

sha512sums="
ee0bba2fc5c62e8ec52d27743871d79c1c201b327f8b6f2c12434fef38c87179485a958bf08b97754aae4a8e82b1d20e4cea74a17e973b0682f78cfa73dff3ef  moosefs-3.0.117.tar.gz
c698aff4de9aeb76202a809e44ac8d0ec9c6348a806b1c813c2a40858339b1c139a480a9c5aff40bf2c9821883c6c0dfeabb010f3faa5746673235f0fb3c5a76  moosefs-cgiserv.initd
b5c625f0004df33889de60ddad37e41f3acf081b37247606a1544e5f63354e121fe4cce511a6e60f4f2c0305155faf0614b8a4bce7267929fe68a1a4b546b582  moosefs-cgiserv.confd
a041fa324d37bda098ad65e9d6507f281ed388471956ca79aa33b8b0d1c4a9d528662a2410c47f3856183d6378ac7fb417c3d7ec314f624e7e5dac7c5e4247f0  moosefs-master.initd
8e73a7715e7a010d435efb9058ed67aad1caa95c840d3f1fae6bafd2a700cedbb0244d29363929d8e8d9a2036a2e0b173c0c2f7b52529ba4088e1296479fc0f5  moosefs-metalogger.initd
56a714f3d29030ca12dc1859a1121b2a89606a2ccccec65481e2eb0b846757dea71dfe5f5a6d648b32e731d14b7d7725f79703851365b0862d7eb415a5f49dfb  moosefs-chunkserver.initd
"
